package spring_test.dao.imp;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import spring_test.dao.PersonDao;
import spring_test.entity.Person;

public class PersonDaoImpl implements PersonDao {
	private MongoTemplate mongoTemplate;

	public void createCollection() {
		if (!this.mongoTemplate.collectionExists(Person.class)) {
			this.mongoTemplate.createCollection(Person.class);
		}
	}

	public List<Person> findAll() {
		return getMongoTemplate().find(new Query(), Person.class);

	}

	@Override
	public void findAndModify(Person person) {

		Update update = new Update();
		update.set("age", person.getAge());
		update.set("name", person.getName());
		getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(person.getId())), update, Person.class);

	}

	@Override
	public List<Person> findByRegex(String regex) {
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Criteria criteria = new Criteria("name").regex(pattern.toString());
		return getMongoTemplate().find(new Query(criteria), Person.class);

	}

	@Override
	public Person findOne(String id) {
		return getMongoTemplate().findOne(new Query(Criteria.where("id").is(id)), Person.class);

	}

	@Override
	public void insert(Person person) {
		getMongoTemplate().insert(person);
	}

	@Override
	public void removeAll() {
		List<Person> list = this.findAll();
		if (list != null) {
			for (Person person : list) {
				getMongoTemplate().remove(person);
			}
		}

	}

	@Override
	public void removeOne(String id) {
		Criteria criteria = Criteria.where("id").in(id);
		if (criteria == null) {
			Query query = new Query(criteria);
			if (query != null && getMongoTemplate().findOne(query, Person.class) != null)
				getMongoTemplate().remove(getMongoTemplate().findOne(query, Person.class));
		}

	}

	/**
	 * @return the mongoTemplate
	 */
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	/**
	 * @param mongoTemplate
	 *            the mongoTemplate to set
	 */
	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

}
